﻿using System;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;

namespace Kaesoft.Hospedaje.Proxy
{
    public class MessageInspector : IClientMessageInspector
    {
        const string WS_NAMESPACE = "urn:LOBApplicationFramework.Security";

        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
        }

        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            if (!string.IsNullOrEmpty(WcfProxyInvoker.Usuario))
            {
                //Añade las credenciales de usuario al header de la trama
                MessageHeader header = MessageHeader.CreateHeader(typeof(string).Name, WS_NAMESPACE, WcfProxyInvoker.Usuario, false);
                request.Headers.Add(header);
            }
            return null;
        }
    }
}